 #include "chooselevel.h"
#include <QMenuBar>
#include <QPainter>
#include <QPixmap>
#include <mypushbutton.h>
#include <QDebug>
#include <QLabel>
#include <QTimer>
ChooseLevel::ChooseLevel(QWidget *parent)
    : QMainWindow{parent}
{
    this->setFixedSize(320, 588);
    this->setWindowIcon(QPixmap(":/myres/Coin0001.png"));
    this->setWindowTitle("choose Level");
    QMenuBar* bar = menuBar();
    setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("start");
    QAction* quitAction = startMenu->addAction("quit");
    connect(quitAction, &QAction::triggered, [&](){
        close();
    });
    //返回按钮
    MyPushButton* returnbtn = new MyPushButton(":/myres/BackButton.png", ":/myres/BackButtonSelected.png");
    returnbtn->setParent(this);
    returnbtn->move(width() - returnbtn->width(), height() - returnbtn->height());
    connect(returnbtn, &QPushButton::clicked, [=](){
        qDebug() << "press" ;
        //发送信号告诉主场景返回类;
        emit this->returnToMain();
    });

    //选择关卡的按钮
    for(int i = 0; i < 20; ++i)
    {
        MyPushButton* menuBtn = new MyPushButton(":/myres/LevelIcon.png");
        //menuBtn->setStyleSheet("QPushButton { text-align: left; }");
        //menuBtn->setText(QString("%1").arg(i+1));
        QLabel* ql = new QLabel();
        menuBtn->setParent(this);
        menuBtn->move(i % 4 * 70 + 25, 130 + i / 4 * 70);
        ql->setParent(this);
        ql->setFixedSize(menuBtn->width(), menuBtn->height());
        ql->setText(QString::number(i + 1));
        ql->move(i % 4 * 70 + 25, 130 + i / 4 * 70);
        ql->setAlignment(Qt::AlignCenter);//设置文字对齐方式

        ql->setAttribute(Qt::WA_TransparentForMouseEvents);//设置ql可被鼠标穿透

        connect(menuBtn, &QPushButton::clicked, [=](){
            //QString s = QString("choose %1").arg(i+1);
            menuBtn->zoom();
            qDebug() << QString("choose %1").arg(i+1);

            ps = new PlayScene(i+1);
            connect(ps, &PlayScene::returnToSelect,[=](){
                this->setGeometry(ps->geometry());
                this->show();
                delete ps;
                ps = nullptr;
            });

            QTimer::singleShot(200, this, [=](){
                ps->setGeometry(this->geometry());
                this->hide();
                ps->show();
            });//延时
            ps->show();
        });
    }


}
void ChooseLevel::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/myres/OtherSceneBg.png");
    painter.drawPixmap(0, 0, width(), height(), pix);

    pix.load(":/myres/Title.png");
    pix = pix.scaled(pix.width() , pix.height() );//缩放pixmap
    painter.drawPixmap(10, 30, pix);//添加背景图
}
